package com.fish.spider.util;

import cn.hutool.core.date.*;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

@Slf4j
public class DateUtils {
    public static List<String> generaDay(String startDateStr, String endDateStr) {

        DateTime startDate = DateUtil.parse(startDateStr, "yyyy-MM-dd");
        DateTime endDate = DateUtil.parse(endDateStr, "yyyy-MM-dd");
        long between = DateUtil.between(startDate, endDate, DateUnit.DAY);
        List<String> list = Lists.newArrayList();
        list.add(DateUtil.format(startDate.toJdkDate(), "yyyy-MM-dd"));
        for (int i = 0; i < between; i++) {
            DateTime offset = DateUtil.offset(startDate.toJdkDate(), DateField.DAY_OF_YEAR, i + 1);
            list.add(DateUtil.format(offset.toJdkDate(), "yyyy-MM-dd"));
        }
        return list;
    }

    /**
     * @param startDateStr
     * @param endDateStr
     * @param flag         true 周日为一周起始，false周一为一周起始
     * @return
     */
    public static List<String> generaWeek(String startDateStr, String endDateStr, boolean flag) {
        List<String> list = Lists.newArrayList();
        Date startDate = DateUtil.parse(startDateStr, "yyyy-MM-dd");
        DateTime endDate = DateUtil.parse(endDateStr, "yyyy-MM-dd");
        DateTime initDate = DateUtil.beginOfWeek(startDate, flag);
        long between = DateUtil.between(startDate, endDate, DateUnit.WEEK);
        between+=1;
        for (int i = 0; i < between; i++) {
            DateTime offset = DateUtil.offset(initDate.toJdkDate(), DateField.DAY_OF_YEAR, 6);
            list.add(DateUtil.format(initDate, "yyyy-MM-dd") + "~" + DateUtil.format(offset, "yyyy-MM-dd"));
            initDate = DateUtil.offset(offset.toJdkDate(), DateField.DAY_OF_YEAR, 1);
        }
        if(initDate.compareTo(endDate)<0){
            list.add(DateUtil.format(initDate, "yyyy-MM-dd") + "~" + endDateStr);
        }
        return list;
    }

    public static void main(String[] args) {
        List<String> list = generaWeek("2011-01-01", "2024-06-06", true);
        log.info(new Gson().toJson(list));
    }
}
